package com.huluobo.a2201mvvm_imdemo.adapter;

import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.huluobo.a2201mvvm_imdemo.R;
import com.huluobo.a2201mvvm_imdemo.chat.ChatActivity;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMTextMessageBody;
import com.hyphenate.util.DateUtils;

import java.util.Date;
import java.util.List;

/**
 * Created by LC on 2024/4/3.
 */
public class ConversationAdapter extends BaseQuickAdapter<EMConversation, BaseViewHolder> {
    public ConversationAdapter(@Nullable List<EMConversation> data) {
        super(R.layout.item_conversation, data);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder baseViewHolder, EMConversation emConversation) {
        //会话列表的用户名/群名
        baseViewHolder.setText(R.id.item_conversation_name, emConversation.conversationId());

        //最后一条消息
        if (emConversation.getLastMessage().getType() == EMMessage.Type.TXT) {
            //文本消息,直接赋值
            EMTextMessageBody body = (EMTextMessageBody) emConversation.getLastMessage().getBody();
            baseViewHolder.setText(R.id.item_conversation_lastMessage, body.getMessage());
        } else if (emConversation.getLastMessage().getType() == EMMessage.Type.IMAGE) {
            baseViewHolder.setText(R.id.item_conversation_lastMessage, "[图片]");
        } else if (emConversation.getLastMessage().getType() == EMMessage.Type.FILE) {
            baseViewHolder.setText(R.id.item_conversation_lastMessage, "[文件]");
        } else {
            baseViewHolder.setText(R.id.item_conversation_lastMessage, "其他消息");
        }
        //最后一条消息的时间戳
        //emConversation.getLastMessage().getMsgTime() 获取最后一条消息的发送时间,当前是一个long
        //通过系统的newDate转换为一个时间格式,此时long转换为了一个Date格式
        //通过环信的DateUtils,getTimestampString 转换为一个String格式,就可以赋值了
        String timestampString = DateUtils.getTimestampString(new Date(emConversation.getLastMessage().getMsgTime()));
        baseViewHolder.setText(R.id.item_conversation_timestamp, timestampString);

        //未读消息数量
        if (emConversation.getUnreadMsgCount() > 0) {
            baseViewHolder.getView(R.id.item_conversation_unreadCount).setVisibility(View.VISIBLE);
            baseViewHolder.setText(R.id.item_conversation_unreadCount, emConversation.getUnreadMsgCount() + "");
        } else {
            baseViewHolder.getView(R.id.item_conversation_unreadCount).setVisibility(View.GONE);
        }

        baseViewHolder.getView(R.id.item_conversation_cl).setOnClickListener(v -> {
            String conversationId = emConversation.conversationId();
            Intent intent = new Intent(getContext(), ChatActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString("conversationId", conversationId);
            int chatType = 0;
            switch (emConversation.getLastMessage().getChatType()) {
                case Chat:
                    chatType = 1;
                    break;
                case GroupChat:
                    chatType = 2;
                    break;
            }
            bundle.putInt("chatType", chatType);
            intent.putExtras(bundle);
            getContext().startActivity(intent);
        });
    }
}
